package com.jingdianjichi.subject.domain.handler.subject;


import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 题目类型的工厂
 * @return:
 * @Author:  sky
 * @date:  2024/11/8 12:09
 */
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {

    /**
     * @Description: 项目启动后，会自动将SubjectTypeHandler的四个实现类，全部放到list里面
     * @return:
     * @Author:  sky
     * @date:  2024/11/8 12:11
     */
    @Autowired
    private List<SubjectTypeHandler> subjectTypeHandlerList;

    private Map<SubjectInfoTypeEnum,SubjectTypeHandler> handlerMap = new HashMap<>();

    /**
     * @Description: 传入type，获取对应的SubjectTypeHandler对象，以进一步创建对应的题目
     * @return:
     * @Author:  sky
     * @date:  2024/11/8 15:26
     */
    public SubjectTypeHandler getHandler(int subjectType){
        SubjectInfoTypeEnum subjectInfoTypeEnum = SubjectInfoTypeEnum.getByCode(subjectType);
        return handlerMap.get(subjectInfoTypeEnum);
    }

    /**
     * @Description: 实现InitializingBean接口，在Bean初始化之后，配置相关内容
     * @return:
     * @Author:  sky
     * @date:  2024/11/8 15:32
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        for(SubjectTypeHandler subjectTypeHandler : subjectTypeHandlerList){
            handlerMap.put(subjectTypeHandler.getHandlerType(),subjectTypeHandler);
        }
    }
}
